package cn.wk.product.service.impl;

import cn.wk.basic.service.impl.BaseServiceImpl;
import cn.wk.product.domain.Product;
import cn.wk.product.domain.ProductDetail;
import cn.wk.product.mapper.ProductDetailMapper;
import cn.wk.product.mapper.ProductMapper;
import cn.wk.product.service.IProductService;
import org.apache.ibatis.annotations.Update;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ProductServiceImpl extends BaseServiceImpl<Product> implements IProductService {
    @Autowired
    private ProductDetailMapper productDetailMapper;
    @Autowired
    private ProductMapper productMapper;

    /**
     * 添加数据
     * @param product
     */
    @Override
    public void save(Product product) {
        //先保存product，保存之后才有id
        super.save(product);
        //保存productdetail
        ProductDetail productDetail = product.getProductDetail();
        productDetail.setProduct(product);
        productDetailMapper.save(productDetail);
    }

    /**
     * 修改
     */
    @Override
    public void update(Product product) {
        //修改product
        super.update(product);
        //再获取product中的数据去修改productdemil
        ProductDetail productDetail = product.getProductDetail();
        //把id要传过去
        productDetail.setProduct(product);
        productDetailMapper.update(productDetail);

    }

    /**
     * 上架服务
     * @param products
     */
    @Override
    public void onsale(List<Product> products) {
        productMapper.onsale(products);
    }

    /**
     * 下架服务
     * @param products
     */
    @Override
    public void offsale(List<Product> products) {
        productMapper.offsale(products);
    }

    /**
     * 删除服务，要把简介和预约须知一起删除
     */
    @Override
    public void delete(Long id) {
        //删除product
        super.delete(id);
        //删除productdetail
        productDetailMapper.delete(id);
    }

    @Override
    public void delBatch(List<Product> products) {
        //批量删除服务
        super.delBatch(products);
        //delBatch
        productDetailMapper.delBatchByproduct(products);
    }
}
